package im.redis.client.operations;

import im.redis.client.JedisTemplate;
import im.redis.client.JedisTemplate.JedisAction;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;

public class PubSubOperation {
	private JedisTemplate template;

	public PubSubOperation(JedisTemplate template) {
		super();
		this.template = template;
	}

	/**
	 * 返回接收到信息 message 的订阅者数量
	 */
	public Long publish(final String channel, final String message) {
		return template.execute(new JedisAction<Long>() {
			@Override
			public Long action(Jedis jedis) {
				return jedis.publish(channel, message);
			}
		});
	}

	public void subscribe(final JedisPubSub messageListener, final String... channels) {
		template.getJedisPool().getResource().subscribe(messageListener, channels);
	}

}
